<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/4/6
 * Time: 18:16
 */
namespace services\models;

use Yii;


class CommentsRelation extends \common\models\CommentsRelation
{
    /*
     * 从redis取商品总、好、中、差评ID列表
     * */
    public function getGoodsCommentsIds($goods_id, $type=''){
        if($type == 'A'){
            $IdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_A_' . $goods_id, '+inf', '-inf');
        }
        elseif($type == 'B'){
            $IdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_B_' . $goods_id, '+inf', '-inf');
        }
        elseif($type == 'C'){
            $IdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_C_' . $goods_id, '+inf', '-inf');
        }
        else{
            $IdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_'.$goods_id,'+inf','-inf');//取商品ID对应的评论ID列表
        }

        return $IdList;
    }

    /*
     * 通过商品评论ID列表取商品评论列表
     * */
    public function getGoodsCommentsByIds($ids){

        $commentsList[] = array();
        foreach($ids as $k => $id){
            $commentsList[$k] = Yii::$app->redis_comments->hgetall('comment_'.$id);
            $commentsList[$k]['content'] = Yii::$app->redis_comments->get('comment_content_' . $id);

            $commentsList[$k]['reply_content'] = '';
            $commentsList[$k]['reply_time'] = '';
            if(isset($commentsList[$k]['is_reply']) && 1 == $commentsList[$k]['is_reply']){
                $reply = Yii::$app->redis_comments->hgetall('comment_reply_' . $id);
                if(!empty($reply)) {
                    $commentsList[$k]['reply_content'] = $reply['content'];
                    $commentsList[$k]['reply_time'] = $reply['update_time'];
                }
            }
            if(!empty($commentsList[$k]['img']))
                $commentsList[$k]['img'] = UpFileData::showImages($commentsList[$k]['img'],0);//取图片地址
            else
                $commentsList[$k]['img'] = array();

        }

        return $commentsList;

    }


    /*
     * 从redis取商品评论列表
     * */
    public function getGoodsCommentsFromRedis($goods_id){

        $commentIdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_'.$goods_id,'+inf','-inf');//取商品ID对应的评论ID列表


        $commentsList[] = array();
        foreach($commentIdList as $k => $id){
            $commentsList[$k] = Yii::$app->redis_comments->hgetall('comment_'.$id);
            $commentsList[$k]['content'] = Yii::$app->redis_comments->get('comment_content_' . $id);

            $commentsList[$k]['reply_content'] = '';
            $commentsList[$k]['reply_time'] = '';
            if(isset($commentsList[$k]['is_reply']) && 1 == $commentsList[$k]['is_reply']){
                $reply = Yii::$app->redis_comments->hgetall('comment_reply_' . $id);
                if(!empty($reply)) {
                    $commentsList[$k]['reply_content'] = $reply['content'];
                    $commentsList[$k]['reply_time'] = $reply['update_time'];
                }
            }
            if(!empty($commentsList[$k]['img']))
                $commentsList[$k]['img'] = UpFileData::showImages($commentsList[$k]['img'],0);//取图片地址
            else
                $commentsList[$k]['img'] = array();

        }

        return $commentsList;

    }

    /*
     * 从redis取商品好中差评率
     * */
    public function getFeedbackRate($goods_id){

        $goodRate['a'] = intval(Yii::$app->redis_comments->get('fb_rate_A_' . $goods_id));
        $goodRate['b'] = intval(Yii::$app->redis_comments->get('fb_rate_B_' . $goods_id));
        $goodRate['c'] = intval(Yii::$app->redis_comments->get('fb_rate_C_' . $goods_id));
        return $goodRate;
    }

    /*
     * 从redis取商品好中差评论数
     * */
    public function getFeedbackNumber($goods_id){

        $number['a'] = intval(Yii::$app->redis_comments->get('fb_num_A_' . $goods_id));
        $number['b'] = intval(Yii::$app->redis_comments->get('fb_num_B_' . $goods_id));
        $number['c'] = intval(Yii::$app->redis_comments->get('fb_num_C_' . $goods_id));
        return $number;
    }

    /*
     * 从redis取商品评论总数
     * */
    public function getFeedbackTotal($goods_id){
        return intval(Yii::$app->redis_comments->get('fb_num_' . $goods_id));
    }


    /*从redis取商品好中差评列表*/
    public function getFeedbackList($goods_id, $type){

        if($type == 'A'){
            $IdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_A_' . $goods_id, '+inf', '-inf');
        }
        elseif($type == 'B'){
            $IdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_B_' . $goods_id, '+inf', '-inf');
        }
        elseif($type == 'C'){
            $IdList = Yii::$app->redis_comments->zrevrangebyscore('goods_comment_C_' . $goods_id, '+inf', '-inf');
        }
        else{
            return false;
        }
        $list[] = array();
        foreach($IdList as $k => $id){
            $list[$k] = Yii::$app->redis_comments->hgetall('comment_'.$id);
            $list[$k]['content'] = Yii::$app->redis_comments->get('comment_content_' . $id);
            if(!empty($list[$k]['img']))
                $list[$k]['img'] = UpFileData::showImages($list[$k]['img'],0);//取图片地址
            else
                $list[$k]['img'] = array();
        }

        return $list;
    }

    /*
     * 从redis取个人中心评论ID列表
     * */
    public function getOwnCommentsIds($bestry_uid,$condition){
        if(1 == $condition['is_perfect']){

            $commentIdList = Yii::$app->redis_comments->zrevrangebyscore('user_comment_perfect_'.$bestry_uid,'+inf','-inf');//取用户ID对应的精华评论ID列表

        }
        elseif(1 == $condition['is_reply']){
            $commentIdList = Yii::$app->redis_comments->zrevrangebyscore('user_comment_reply_'.$bestry_uid,'+inf','-inf');//取用户ID对应的客服回复评论ID列表

        }
        else{
            $commentIdList = Yii::$app->redis_comments->zrevrangebyscore('user_comment_'.$bestry_uid,'+inf','-inf');//取所有评论ID列表
        }

        return $commentIdList;
    }


    /*
     * 从redis取个人中心评论列表
     * */
    public function getOwnCommentsFromRedis($commentIdList){

        if(empty($commentIdList)) return false;

        foreach($commentIdList as $k => $id){
            $commentsList[$k] = Yii::$app->redis_comments->hgetall('comment_'.$id);

            $commentsList[$k]['content'] = Yii::$app->redis_comments->get('comment_content_' . $id);

/*            $goods_info = (Yii::$app->redis_goods->hmget('goods_' . $commentsList[$k]['goods_id'], array('goods_name')));
            $commentsList[$k]['goods_name'] = !empty($goods_info['goods_name'])?$goods_info['goods_name']:'商品名称';//取商品名称*/
            $info = GoodsDetail::findByGoodsId($commentsList[$k]['goods_id'],'goods_name');

            $commentsList[$k]['goods_name'] = isset($info['goods_name'])?$info['goods_name']:'';

            if(isset($commentsList[$k]['is_reply']) && 1 == $commentsList[$k]['is_reply']){
                $reply = Yii::$app->redis_comments->hgetall('comment_reply_' . $id);
                if(!empty($reply)) {
                    $commentsList[$k]['reply_content'] = $reply['content'];
                    $commentsList[$k]['reply_time'] = $reply['update_time'];
                }
            }
            if(!empty($commentsList[$k]['img'])) {
                //var_dump($commentsList[$k]['img']);
                $commentsList[$k]['img'] = UpFileData::showImages($commentsList[$k]['img'], 0);//取图片地址
            }else {
                $commentsList[$k]['img'] = array();
            }
            //var_dump($commentsList[$k]['img']);die;
        }
        if(!isset($commentsList)){
            $commentsList[] = array();
        }

        return $commentsList;
    }

    /*从redis取评论条数*/
    public function getOwnCommentsNumberFromRedis($bestry_uid){

        //$numArr = Yii::$app->redis_comments->hgetall('user_comment_number_' . $bestry_uid);
        $number['all'] = Yii::$app->redis_comments->get('u_comment_num_' . $bestry_uid);
        $number['perfect'] = Yii::$app->redis_comments->get('u_comment_num_p_' . $bestry_uid);
        $number['reply'] = Yii::$app->redis_comments->get('u_comment_num_r_' . $bestry_uid);
        return $number;
    }

}